@echo off
setlocal enabledelayedexpansion

echo ================================================
echo Property Cleaning System - Manual Installation
echo No Docker Required - Direct Deployment
echo ================================================
echo.

:: Check admin rights
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo ERROR: Please run as administrator!
    pause
    exit /b 1
)

echo [1/5] Checking system requirements...

:: Check Windows version compatibility
ver | find "Windows 10" >nul
if %errorlevel% equ 0 (
    echo [OK] Windows 10 detected
) else (
    ver | find "Windows 11" >nul
    if %errorlevel% equ 0 (
        echo [OK] Windows 11 detected
    ) else (
        echo [WARNING] Older Windows version - may have compatibility issues
    )
)

echo [2/5] Installing Java Runtime Environment...
where java >nul 2>&1
if %errorlevel% neq 0 (
    echo Installing Java 8...
    powershell -Command "
    Write-Host 'Downloading Java 8...' -ForegroundColor Green
    $url = 'https://javadl.oracle.com/webapps/download/AutoDL?BundleId=248242_ce59cff5c23f4e2eaf4e778a117d4c5b'
    $output = 'java-installer.exe'
    Invoke-WebRequest -Uri $url -OutFile $output
    Write-Host 'Installing Java...' -ForegroundColor Yellow
    Start-Process -Wait -FilePath $output -Args '/s'
    Remove-Item $output
    Write-Host 'Java installation complete!' -ForegroundColor Green
    "
) else (
    echo Java already installed
)

echo [3/5] Installing Node.js...
where node >nul 2>&1
if %errorlevel% neq 0 (
    echo Installing Node.js 18 LTS...
    powershell -Command "
    Write-Host 'Downloading Node.js...' -ForegroundColor Green
    $url = 'https://nodejs.org/dist/v18.18.2/node-v18.18.2-x64.msi'
    $output = 'node-installer.msi'
    Invoke-WebRequest -Uri $url -OutFile $output
    Write-Host 'Installing Node.js...' -ForegroundColor Yellow
    Start-Process -Wait -FilePath msiexec.exe -Args '/i node-installer.msi /quiet'
    Remove-Item $output
    Write-Host 'Node.js installation complete!' -ForegroundColor Green
    "
) else (
    echo Node.js already installed
)

echo [4/5] Setting up MySQL database...

:: Create MySQL installer
echo @echo off > mysql-setup.bat
echo echo MySQL 8.0 Setup Guide... >> mysql-setup.bat
echo echo. >> mysql-setup.bat
echo echo STEP 1: Download MySQL 8.0 Community Server >> mysql-setup.bat
echo echo    URL: https://dev.mysql.com/downloads/installer/ >> mysql-setup.bat
echo echo. >> mysql-setup.bat
echo echo STEP 2: Run installer with these settings: >> mysql-setup.bat
echo echo    - Setup Type: Server only >> mysql-setup.bat
echo echo    - Port: 3306 >> mysql-setup.bat
echo echo    - Root Password: root123 >> mysql-setup.bat
echo echo    - Service Name: MySQL80 >> mysql-setup.bat
echo echo. >> mysql-setup.bat
echo echo STEP 3: After installation, run: >> mysql-setup.bat
echo echo    mysql -u root -p ^< database\schema.sql >> mysql-setup.bat
echo echo. >> mysql-setup.bat
echo pause >> mysql-setup.bat

echo [5/5] Creating project structure and startup scripts...

:: Create project directory
set "PROJECT_DIR=%USERPROFILE%\property-cleaning-manual"
if not exist "%PROJECT_DIR%" (
    mkdir "%PROJECT_DIR%"
)

:: Copy project files
echo Copying project files...
xcopy /E /I /Y backend "%PROJECT_DIR%\backend\" >nul 2>&1
xcopy /E /I /Y frontend "%PROJECT_DIR%\frontend\" >nul 2>&1
xcopy /E /I /Y database "%PROJECT_DIR%\database\" >nul 2>&1
copy docker-compose.yml "%PROJECT_DIR%\" >nul 2>&1

:: Create startup script
echo @echo off > "%PROJECT_DIR%\start-system.bat"
echo echo ================================================ >> "%PROJECT_DIR%\start-system.bat"
echo echo Property Cleaning System - Manual Start >> "%PROJECT_DIR%\start-system.bat"
echo echo ================================================ >> "%PROJECT_DIR%\start-system.bat"
echo echo. >> "%PROJECT_DIR%\start-system.bat"
echo echo [1/3] Starting MySQL database... >> "%PROJECT_DIR%\start-system.bat"
echo net start MySQL80 >> "%PROJECT_DIR%\start-system.bat"
echo timeout /t 5 /nobreak ^>nul >> "%PROJECT_DIR%\start-system.bat"
echo. >> "%PROJECT_DIR%\start-system.bat"
echo [2/3] Starting Java backend... >> "%PROJECT_DIR%\start-system.bat"
echo cd backend >> "%PROJECT_DIR%\start-system.bat"
echo start "" cmd /c "mvnw spring-boot:run ^> backend.log 2^>^&1" >> "%PROJECT_DIR%\start-system.bat"
echo cd .. >> "%PROJECT_DIR%\start-system.bat"
echo timeout /t 10 /nobreak ^>nul >> "%PROJECT_DIR%\start-system.bat"
echo. >> "%PROJECT_DIR%\start-system.bat"
echo [3/3] Starting Node.js frontend... >> "%PROJECT_DIR%\start-system.bat"
echo cd frontend >> "%PROJECT_DIR%\start-system.bat"
echo call npm install >> "%PROJECT_DIR%\start-system.bat"
echo start "" cmd /c "npm run dev ^> frontend.log 2^>^&1" >> "%PROJECT_DIR%\start-system.bat"
echo cd .. >> "%PROJECT_DIR%\start-system.bat"
echo timeout /t 5 /nobreak ^>nul >> "%PROJECT_DIR%\start-system.bat"
echo. >> "%PROJECT_DIR%\start-system.bat"
echo echo System started! >> "%PROJECT_DIR%\start-system.bat"
echo echo Backend API: http://localhost:8080/swagger-ui.html >> "%PROJECT_DIR%\start-system.bat"
echo echo Frontend: http://localhost:3000 >> "%PROJECT_DIR%\start-system.bat"
echo echo. >> "%PROJECT_DIR%\start-system.bat"
echo echo Logs: backend.log and frontend.log >> "%PROJECT_DIR%\start-system.bat"
echo pause >> "%PROJECT_DIR%\start-system.bat"

:: Create stop script
echo @echo off > "%PROJECT_DIR%\stop-system.bat"
echo echo Stopping Property Cleaning System... >> "%PROJECT_DIR%\stop-system.bat"
echo taskkill /F /FI "WindowTitle eq *Java*" ^>nul 2^>^&1 >> "%PROJECT_DIR%\stop-system.bat"
echo taskkill /F /FI "WindowTitle eq *Node*" ^>nul 2^>^&1 >> "%PROJECT_DIR%\stop-system.bat"
echo net stop MySQL80 >> "%PROJECT_DIR%\stop-system.bat"
echo echo System stopped! >> "%PROJECT_DIR%\stop-system.bat"
echo pause >> "%PROJECT_DIR%\stop-system.bat"

:: Create status script
echo @echo off > "%PROJECT_DIR%\status.bat"
echo echo System Status Check... >> "%PROJECT_DIR%\status.bat"
echo echo. >> "%PROJECT_DIR%\status.bat"
echo echo MySQL Service: >> "%PROJECT_DIR%\status.bat"
echo sc query MySQL80 ^| find "STATE" >> "%PROJECT_DIR%\status.bat"
echo. >> "%PROJECT_DIR%\status.bat"
echo echo Java Backend: >> "%PROJECT_DIR%\status.bat"
echo tasklist ^| find "java" >> "%PROJECT_DIR%\status.bat"
echo. >> "%PROJECT_DIR%\status.bat"
echo echo Node.js Frontend: >> "%PROJECT_DIR%\status.bat"
echo tasklist ^| find "node" >> "%PROJECT_DIR%\status.bat"
echo. >> "%PROJECT_DIR%\status.bat"
echo echo Port Status: >> "%PROJECT_DIR%\status.bat"
echo netstat -an ^| find ":3306" ^| find "LISTEN" >> "%PROJECT_DIR%\status.bat"
echo netstat -an ^| find ":8080" ^| find "LISTEN" >> "%PROJECT_DIR%\status.bat"
echo netstat -an ^| find ":3000" ^| find "LISTEN" >> "%PROJECT_DIR%\status.bat"
echo pause >> "%PROJECT_DIR%\status.bat"

:: Create desktop shortcuts
echo Set oWS = WScript.CreateObject("WScript.Shell") > "%PROJECT_DIR%\create-shortcuts.vbs"
echo sLinkFile = oWS.SpecialFolders("Desktop") ^& "\Property Cleaning System (Manual).lnk" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.TargetPath = "%PROJECT_DIR%\start-system.bat" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.WorkingDirectory = "%PROJECT_DIR%" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.Description = "Property Cleaning System (Manual Deployment)" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.IconLocation = "%SYSTEMROOT%\System32\shell32.dll,21" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.Save >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo Set oLink = Nothing >> "%PROJECT_DIR%\create-shortcuts.vbs"

"%PROJECT_DIR%\create-shortcuts.vbs"
del "%PROJECT_DIR%\create-shortcuts.vbs"

echo.
echo ================================================
echo Manual Installation Complete!
echo ================================================
echo.
echo Next Steps:
echo 1. Run: mysql-setup.bat (to install MySQL)
echo 2. After MySQL setup, run: start-system.bat
echo 3. Or use desktop shortcut: Property Cleaning System (Manual)
echo.
echo Project directory: %PROJECT_DIR%
echo MySQL setup guide: mysql-setup.bat
pause